package com.sicau.socialpracticesystem.controller;


import com.sicau.socialpracticesystem.common.constant.UrlConstants;
import com.sicau.socialpracticesystem.common.returning.ResultData;
import com.sicau.socialpracticesystem.config.aop.ValidateAspect;
import com.sicau.socialpracticesystem.entity.param.PageParam;
import com.sicau.socialpracticesystem.entity.param.practicetype.PracticeTypeAddParam;
import com.sicau.socialpracticesystem.entity.param.practicetype.PracticeTypeQueryParam;
import com.sicau.socialpracticesystem.entity.param.practicetype.PracticeTypeUpdateParam;
import com.sicau.socialpracticesystem.entity.po.PracticeType;
import com.sicau.socialpracticesystem.entity.vo.PageDataVO;
import com.sicau.socialpracticesystem.entity.vo.practicetype.PracticeTypeVO;
import com.sicau.socialpracticesystem.service.PracticeTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.dozermapper.core.Mapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 社会实践类型表 前端控制器
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2021-04-26
 */
@RestController
@Api(tags = "社会实践类型相关接口")
@PreAuthorize("isAuthenticated()")
@RequestMapping(UrlConstants.PRACTICE_TYPE_PREFIX)
public class PracticeTypeController {

    @Resource
    private PracticeTypeService practiceTypeService;

    @Resource
    private Mapper dozerMapper;

    @PostMapping
    @ApiOperation("添加一个社会实践类型")
    @ValidateAspect.ParamValidate()
    public ResultData<Object> addPracticeType(@RequestBody @Valid PracticeTypeAddParam practiceTypeAddParam,
                                              @ApiParam(hidden = true)
                                              @SuppressWarnings("unused") BindingResult bindingResult) {

        PracticeType practiceType = dozerMapper.map(practiceTypeAddParam, PracticeType.class);
        return practiceTypeService.save(practiceType)
                ? new ResultData<>().OK()
                : new ResultData<>().FAILED();
    }

    @GetMapping
    @ApiOperation("查询社会实践类型(不带分页)")
    public ResultData<List<PracticeTypeVO>> getPrizes(PracticeTypeQueryParam practiceTypeQueryParam) {
        List<PracticeTypeVO> practiceTypeVOS = practiceTypeService.queryPrizeByConditions(practiceTypeQueryParam);
        return new ResultData<List<PracticeTypeVO>>().OK().setObj(practiceTypeVOS);
    }

    @GetMapping("/page")
    @ApiOperation("查询社会实践类型(带分页)")
    public ResultData<PageDataVO<PracticeTypeVO>> getPrizes(PracticeTypeQueryParam practiceTypeQueryParam, PageParam pageParam) {
        PageDataVO<PracticeTypeVO> practiceTypeVOS = practiceTypeService.queryPrizeByConditions(practiceTypeQueryParam, pageParam);
        return new ResultData<PageDataVO<PracticeTypeVO>>().OK().setObj(practiceTypeVOS);
    }

    @DeleteMapping
    @ApiOperation("删除一个或多个社会实践类型")
    public ResultData<Object> removePrizes(@RequestParam("ids") ArrayList<Integer> ids) {
        return practiceTypeService.removeByIds(ids)
                ? new ResultData<>().OK()
                : new ResultData<>().FAILED().setResultIns("部分或全部数据删除失败, 可能有的数据已经被删除了");
    }

    @PutMapping("/{id}")
    @ApiOperation("修改社会实践类型")
    @ValidateAspect.ParamValidate()
    public ResultData<Object> updatePrize(@PathVariable String id,
                                          @RequestBody @Valid PracticeTypeUpdateParam practiceTypeUpdateParam,
                                          @ApiParam(hidden = true)
                                          @SuppressWarnings("unused") BindingResult bindingResult) {
        PracticeType practiceType = practiceTypeService.getById(id);
        practiceType.setType(practiceTypeUpdateParam.getType());
        return practiceTypeService.updateById(practiceType)
                ? new ResultData<>().OK().setObj(practiceTypeService.getById(id))
                : new ResultData<>().FAILED();
    }
}

